package JavaServlet.TimeServlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/student1")
public class StudentServlet extends HttpServlet{
		@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			System.out.println("StudentServlet.service()");
			
		//	ArrayList<Student> list = new ArrayList<Student>();
//			Student student1 = new Student(1, "張三1", 23, "男");		
//			Student student2 = new Student(2, "張三2", 23, "男");		
//			Student student3 = new Student(3, "張三3", 23, "男");		
//			Student student4 = new Student(4, "張三4", 23, "男");		
//			Student student5 = new Student(5, "張三5", 23, "男");	
//			list.add(student1);
//			list.add(student2);
//			list.add(student3);
//			list.add(student4);
//			list.add(student5);
			
			ArrayList<Student>list = new ArrayList<Student>();
			ResultSet resultSet = null;
			Connection connection = null;
			Statement statement = null;
			try {
				connection = JDBCUtil.getConnection();
				String sql = "select id, name,age,gender from student";
				statement = connection.createStatement();
				resultSet = statement.executeQuery(sql);
				while (resultSet.next()) {
					int id = resultSet.getInt("id");
					String name = resultSet.getString("name");
					int age = resultSet.getInt("age");
					String gender = resultSet.getString("gender");
					int banjiId = resultSet.getInt("banjiId");
					Student student = new Student(id, name, age, gender, banjiId);
					list.add(student);
				}
				for (Student student : list) {
					System.out.println(student);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				JDBCUtil.close(connection, statement, resultSet);
			}
			
			// 放到一块内存区域里面
			req.setAttribute("list", list);
			// 请求转发到student_list.jsp页面进行展示
			req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
//			resp.setContentType("text/html;charset=utf-8");
//			PrintWriter printWriter = resp.getWriter();
//			printWriter.println("<table border='1' cellspacing='0' width='400px' style='margin:auto'>");
//			printWriter.println("	<tr>                                         ");
//			printWriter.println("		<td>ID</td>                              ");
//			printWriter.println("		<td>名字</td>                            ");
//			printWriter.println("		<td>年龄</td>                            ");
//			printWriter.println("		<td>性别</td>                            ");
//			printWriter.println("	</tr>                                        ");
//			for(Student student:list) {	
//				printWriter.println("	<tr>                                         ");
//				printWriter.println("		<td>"+student.getId()+"</td>                               ");
//				printWriter.println("		<td>"+student.getName()+"</td>                           ");
//				printWriter.println("		<td>"+student.getAge()+"</td>                              ");
//				printWriter.println("		<td>"+student.getGender()+"</td>                              ");
//				printWriter.println("	</tr>                                        ");
//			}
//			
//			printWriter.println("</table>                                        ");
//			printWriter.close();                                                
		}                                                                                                    
}
